<template>
  <div class="goods-hot">
    <h3>{{ names[type] }}</h3>
    <GoodsItem
      v-for="item in hotGoods"
      :key="item.id"
      :goods="item"
    ></GoodsItem>
  </div>
</template>
<script>
import GoodsItem from "@/views/category/components/GoodsItem";
import { ref } from "vue";
import { getHotGoodsApi } from "@/api/goods";
import { onBeforeRouteUpdate, useRoute } from "vue-router";
export default {
  name: "GoodsHot",
  props: {
    type: {
      type: Number,
      default: 1,
    },
  },
  components: { GoodsItem },
  setup(props) {
    const { hotGoods } = useHotGoods(props.type);
    // 定义热榜名称
    const names = { 1: "24小时热销榜", 2: "周热销榜", 3: "总热销榜" };
    return {
      hotGoods,
      names,
    };
  },
};
// 获取热榜数据
function useHotGoods(type) {
  const route = useRoute();
  const hotGoods = ref();
  const getData = (id) => {
    getHotGoodsApi({ id, type }).then((data) => {
      hotGoods.value = data.result;
    });
  };
  getData(route.params.id);
  // 当路由更新时重新获取商品评论头部信息
  onBeforeRouteUpdate((to) => getData(to.params.id));
  return {
    hotGoods,
  };
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  :deep(.goods-item) {
    background: #fff;
    width: 100%;
    margin-bottom: 10px;
    img {
      width: 200px;
      height: 200px;
    }
    p {
      margin: 0 10px;
    }
    &:hover {
      transform: none;
      box-shadow: none;
    }
  }
}
</style>
